﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class PersonnelIinformationController : Controller
    {
        private PersonnelIinformationInfo perinfo = new PersonnelIinformationInfo(true);
        public ActionResult PersonnelIinformationIndex()
        {
            ViewData["Deptment"] = GetDeptment();
            ViewData["GroupDept"] = GetGroupDept("-1");
            ViewData["StructrueName"] = GetStructrueName();
            ViewData["SJLY"] = GetSJLY();
            ViewData["KZT"] = GetKZT();
            return View();
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDeptment()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }


        /// <summary>
        /// 获取岗位列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetStructrueName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetStructrueName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["structrueId"]), ds.Tables[0].Rows[i]["structrueName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取数据来源列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetSJLY()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("-1", "全部");
            dic.Add("本职员工", "本职员工");
            dic.Add("外协单位", "外协单位");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 卡状态下拉框绑定
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetKZT()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("-1", "全部");
            dic.Add("0", "正常");
            dic.Add("2", "挂失");
            dic.Add("11", "未发卡");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDept(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        /// <summary>
        /// 获取人事信息列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetPersonnelIinformationList(PageVM pvm)
        {
            string query = " 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query += " and 1=1";
            }
            else
            {
                string condition = pvm.query;
                if (condition.Split(',')[0].ToString() != "-1")
                {
                    query += " and 岗位编号='" + condition.Split(',')[0].ToString() + "'";
                }
                if (condition.Split(',')[1].ToString() != "" && condition.Split(',')[1].ToString() != null)
                {
                    query += " and PName like'%" + condition.Split(',')[1].ToString() + "%'";
                }
                if (condition.Split(',')[2].ToString() != "-1")
                {
                    query += " and DEP_ID='" + condition.Split(',')[2].ToString() + "'";
                }
                if (condition.Split(',')[3].ToString() != "" && condition.Split(',')[3].ToString() != null)
                {
                    query += " and EMPNO='" + condition.Split(',')[3].ToString() + "'";
                }
                if (condition.Split(',')[4].ToString() != "" && condition.Split(',')[4].ToString() != null)
                {
                    query += " and CARDSERIAL=" + condition.Split(',')[4].ToString();
                }
                if (condition.Split(',')[5].ToString() != "-1")
                {
                    query += " and GroupIdDept=" + condition.Split(',')[5].ToString();
                }
                if (condition.Split(',')[6].ToString() != "-1")
                {
                    query += " and 数据来源='" + condition.Split(',')[6].ToString() + "'";
                }
                if (condition.Split(',')[7].ToString() != "-1")
                {
                    query += " and 卡状态编码='" + condition.Split(',')[7].ToString() + "'";
                }
            }
            IList<PersonnelIinformationInfo> perlist = perinfo.SelPersonnelIinformationInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = perinfo.GetPersonnelIinformationCount(query);
            List<string> v;
            foreach (PersonnelIinformationInfo item in perlist)
            {
                #region
                v = new List<string>();
                v.Add(item.PID.ToString());
                v.Add(item.DeptName);
                v.Add(item.StructrueName.ToString());
                v.Add(item.EMPNO.ToString());
                v.Add(item.PName.ToString());
                v.Add(item.CARDSTATUS.ToString());
                v.Add(item.CARDSERIAL.ToString());
                v.Add(item.PERSONTYPENAME.ToString());
                v.Add(item.Sex.ToString());
                v.Add(item.Balance.ToString());
                v.Add(item.WorkStatus.ToString());
                v.Add(item.EnrollDate.ToString());
                v.Add(item.SJLY);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.PID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string structrueId, string PName, string DEP_ID, string EmpNo, string CardSerial, string GroupIdDept, string SJLY, string KZT)
        {
            return EContent(structrueId, PName, DEP_ID, EmpNo, CardSerial, GroupIdDept, SJLY, KZT);
        }

        public ExcelContentResult EContent(string structrueId, string PName, string DEP_ID, string EmpNo, string CardSerial, string GroupIdDept, string SJLY, string KZT)
        {
            string query = " and 1=1 ";
            if (structrueId != "-1")
            {
                query += " and 岗位编号='" + structrueId + "'";
            }
            if (PName != "" && PName != null)
            {
                query += " and PName like'%" + PName + "%'";
            }
            if (DEP_ID != "-1")
            {
                query += " and DEP_ID='" + DEP_ID + "'";
            }
            if (EmpNo != "" && EmpNo != null)
            {
                query += " and EMPNO='" + EmpNo + "'";
            }
            if (CardSerial != "" && CardSerial != null)
            {
                query += " and CARDSERIAL=" + CardSerial;
            }
            if (GroupIdDept != "-1")
            {
                query += " and GroupIdDept=" + GroupIdDept;
            }
            if (SJLY != "-1")
            {
                query += " and 数据来源='" + SJLY + "'";
            }
            if (KZT != "-1")
            {
                query += " and 卡状态编码='" + KZT + "'";
            }
            StringBuilder sBuilder = new StringBuilder();
            DataSet list = perinfo.SelPersonnelIinformationInfoAll(query);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='13' style='font-size:25px; text-align:center;'>从事资料信息</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td>PID</td>");
            sBuilder.Append("<td>部门名称</td>");
            sBuilder.Append("<td>岗位</td>");
            sBuilder.Append("<td>人员编号</td>");
            sBuilder.Append("<td>姓名</td>");
            sBuilder.Append("<td>卡状态</td>");
            sBuilder.Append("<td>卡序列号</td>");
            sBuilder.Append("<td>卡类型</td>");
            sBuilder.Append("<td>性别</td>");
            sBuilder.Append("<td>卡内余额</td>");
            sBuilder.Append("<td>在职状态</td>");
            sBuilder.Append("<td>入职日期</td>");
            sBuilder.Append("<td>数据来源</td>");
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td>" + item["PID"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["DeptName"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["岗位名称"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["EMPNO"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["PName"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["卡状态"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["卡序列号"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["PersonTypeName"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["性别"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["卡内余额"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["工作状态"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["入职日期"].ToString() + "</td>");
                sBuilder.Append("<td>" + item["数据来源"].ToString() + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "renyuanziliao";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
